﻿/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //任意一条路径均可以被看作由某个节点为起点，从其左儿子和右儿子向下遍历的路径拼接得到。
    int ret;
    public int diameterOfBinaryTree(TreeNode root) {
depth(root);
return ret;
    }
    public int depth(TreeNode root){
        if(root==null){
            return 0;
        }
        int L=depth(root.left);//左子树深度
        int R=depth(root.right);//右子树深度
      ret=Math.max(L+R,ret);//每个节点最大直径
      return Math.max(L,R)+1;//返回节点深度
    }
}